package com.zhiliao.common.utils;

import com.alibaba.fastjson.JSONArray;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 短信工具类
 *
 * @author qirong
 * @date 2018-10-26
 */
public class SmsUtil {

    private final static Logger log = LoggerFactory.getLogger(SmsUtil.class);

    private final static String URL = "https://api.netease.im/sms/sendtemplate.action";
    private final static String APPKEY = "cc58c823717404e2bb009fb055f659cf";
    private final static String APPSECRET = "e4a1120b04c5";
    private final static String NONCE =  "12345";

    public static void sendTemplateMsg(String mobile,String templateId,String... params){

        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumUtil.getCheckSum(APPSECRET, NONCE ,curTime);

        JSONArray paramArray = new JSONArray();
        for(String param : params){
            paramArray.add(param);
        }
        Map<String,String> queryMap = new HashMap<>(16);
        queryMap.put("templateid",templateId);
        queryMap.put("mobiles","[\""+mobile+"\"]");
        queryMap.put("params",paramArray.toJSONString());

        HttpResponse response = HttpRequest.post(URL)
                .header("charset","utf-8")
                .header("contentType", "application/x-www-form-urlencoded")
                .header("AppKey", APPKEY)
                .header("Nonce", NONCE)
                .header("CurTime", curTime)
                .header("CheckSum", checkSum)
                .query(queryMap).send();
        log.info(response.bodyText());
    }

}
